package com.shrcl.engineerprocess.domain.impl;

import com.shrcl.engineerprocess.domain.TSCServiceType;
import com.shrcl.engineerprocess.domain.TSCServiceTypeDefinition;
import com.shrcl.engineerprocess.domain.TSCServiceTypeInstance;
import com.shrcl.engineerprocess.domain.impl.TSCServiceTypeInstanceImpl;
import com.shrcl.engineerprocess.domain.proxy.TSCServiceTypeDefinitionProxy;
import edu.fudan.mylang.pf.BaseModelObject;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.pf.IObjectFactory;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.hibernate.annotations.DiscriminatorOptions;

@Entity
@DiscriminatorValue("TSCSERVICETYPEDEFINITION_TYPE")
@DiscriminatorOptions(force = true)
@DiscriminatorColumn(length = 64)
@Table(name = "tscservicetypedefinition")
public class TSCServiceTypeDefinitionImpl extends BaseModelObject implements TSCServiceTypeDefinition {
  public TSCServiceTypeDefinitionImpl() {
    super();
  }
  
  @OneToMany(targetEntity = TSCServiceTypeInstanceImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "typeDefinition")
  private Collection<TSCServiceTypeInstance> values = new java.util.ArrayList<com.shrcl.engineerprocess.domain.TSCServiceTypeInstance>();;
  
  public Collection<TSCServiceTypeInstance> getValues() {
    return this.values;
  }
  
  public TSCServiceTypeInstance createAndAddtoValues() {
    getValues().size();
    com.shrcl.engineerprocess.domain.TSCServiceTypeInstance tSCServiceTypeInstance = new com.shrcl.engineerprocess.domain.impl.TSCServiceTypeInstanceImpl(this);
    getObjectFactory().create(tSCServiceTypeInstance);
    getValues().add(tSCServiceTypeInstance);
    return tSCServiceTypeInstance;
    
  }
  
  public TSCServiceTypeInstance createAndAddtoValues(final Procedure1<TSCServiceTypeInstance> updater) {
    getValues().size();
    com.shrcl.engineerprocess.domain.TSCServiceTypeInstance tSCServiceTypeInstance = new com.shrcl.engineerprocess.domain.impl.TSCServiceTypeInstanceImpl(this);
    getObjectFactory().create(tSCServiceTypeInstance);
    getValues().add(tSCServiceTypeInstance);
    return tSCServiceTypeInstance;
    
  }
  
  public TSCServiceTypeDefinition removeAllValues() {
    for(TSCServiceTypeInstance obj : this.values){
    	getObjectFactory().delete(obj);
    }
    this.values.clear();
    return this;	
    
  }
  
  public TSCServiceTypeDefinition removeFromValues(final TSCServiceTypeInstance tSCServiceTypeInstance) {
    this.values.remove(tSCServiceTypeInstance);
    getObjectFactory().delete(tSCServiceTypeInstance);
    return this;
    
  }
  
  public void init() {IGenericQuery<TSCServiceType> query=getObjectFactory().createGenericQuery(TSCServiceType.class,"select e from com.shrcl.engineerprocess.domain.impl.TSCServiceTypeImpl as e   where  e.active = 1      ");
    Collection<TSCServiceType> _list = query.list();
    final Procedure1<TSCServiceType> _function = new Procedure1<TSCServiceType>() {
        public void apply(final TSCServiceType type) {
          TSCServiceTypeInstance _createAndAddtoValues = TSCServiceTypeDefinitionImpl.this.createAndAddtoValues();
          final Procedure1<TSCServiceTypeInstance> _function = new Procedure1<TSCServiceTypeInstance>() {
              public void apply(final TSCServiceTypeInstance it) {
                it.setServiceType(type);
              }
            };
          ObjectExtensions.<TSCServiceTypeInstance>operator_doubleArrow(_createAndAddtoValues, _function);
        }
      };
    IterableExtensions.<TSCServiceType>forEach(_list, _function);
  }
  
  public void addServiceTypeInstance(final Procedure1<? super TSCServiceTypeInstance> init) {
    TSCServiceTypeInstance _createAndAddtoValues = this.createAndAddtoValues();
    final Procedure1<TSCServiceTypeInstance> _function = new Procedure1<TSCServiceTypeInstance>() {
        public void apply(final TSCServiceTypeInstance it) {
          init.apply(it);
        }
      };
    ObjectExtensions.<TSCServiceTypeInstance>operator_doubleArrow(_createAndAddtoValues, _function);
  }
  
  public static TSCServiceTypeDefinition create(final IObjectFactory objectFactory) {
    com.shrcl.engineerprocess.domain.TSCServiceTypeDefinition tSCServiceTypeDefinition = new com.shrcl.engineerprocess.domain.impl.TSCServiceTypeDefinitionImpl(
    );
    objectFactory.create(tSCServiceTypeDefinition);
    return tSCServiceTypeDefinition;			
    
  }
  
  public static TSCServiceTypeDefinition createTSCServiceTypeDefinitionImpl(final IObjectFactory objectFactory, final Procedure1<TSCServiceTypeDefinition> init) {
    com.shrcl.engineerprocess.domain.TSCServiceTypeDefinition tSCServiceTypeDefinition = new com.shrcl.engineerprocess.domain.impl.TSCServiceTypeDefinitionImpl(
    );
    objectFactory.create(tSCServiceTypeDefinition);
    init.apply(tSCServiceTypeDefinition);
    
    return tSCServiceTypeDefinition;			
    
  }
  
  public TSCServiceTypeDefinitionProxy toProxy() {
    com.shrcl.engineerprocess.domain.proxy.TSCServiceTypeDefinitionProxy proxy = new com.shrcl.engineerprocess.domain.proxy.TSCServiceTypeDefinitionProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
